﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SatisKarsilastirma
{
    public class Depo
    {
        public int No { get; set; }
        public string Adi { get; set; }
        public string Yol { get; set; }
    }

    public class Mikro
    {

        public void SaveDepo(List<Depo> depolar)
        {
            using (StreamWriter sw = new StreamWriter(Application.StartupPath + "\\depolar.txt"))
            {
                foreach (var item in depolar)
                {
                    sw.WriteLine(item.No.ToString() + ";" + item.Yol);
                }
                sw.Close();
            }
        }

        public List<Depo> ListDepo()
        {
            List<Depo> _liste = new List<Depo>();
            using (SqlConnection cn = new SqlConnection(Sessions.Get.CnnString.Replace("_WORKDATA", "")))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT   dep_no, dep_adi FROM         DEPOLAR", cn))
                {
                    SqlDataReader rd = cmd.ExecuteReader();
                    while (rd.Read())
                    {
                        _liste.Add(new Depo()
                        {
                            Adi = rd.GetString(1),
                            No = rd.GetInt32(0),
                            Yol = ""
                        });
                    }
                }
                cn.Close();
            }

            if (File.Exists(Application.StartupPath + "\\depolar.txt"))
            {
                using (StreamReader sr = new StreamReader(Application.StartupPath + "\\depolar.txt"))
                {
                    while (sr.Peek() > -1)
                    {
                        string[] fields = sr.ReadLine().Split(';');
                        Depo dp = _liste.Where(x => x.No == int.Parse(fields[0])).FirstOrDefault();
                        if (dp != null)
                        {
                            dp.Yol = fields[1];
                        }
                    }
                    sr.Close();
                }
            }

            return _liste;
        }
    }
}
